﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;



namespace TheCup.Controllers
{
    public class ScoreController : Controller
    {
        
        [Authorize]
        [HttpPost]
        public ActionResult AddScorecard(int roundId, int playerId, int teeId)
        {
            using (var ctx = new TheCupContext())
            {
                Player p = ctx.Players.Find(playerId);
                Round round = ctx.Rounds.Find(roundId);
                Tee tee = ctx.Tees.Find(teeId);
                
                Scorecard card = new Scorecard { Round = round, Player = p, Tee = tee };
                card.Handicap = p.Hcp;

                for (int i = 1; i < 19; i++)
                {
                    // set some defaults
                    card.SetPuttsForHole(i, 2);
                    card.SetStrokesForHole(i, card.Tee.GetPar(i) + card.GetAssignedStrokes(i));
                }

                return PartialView("_ScoreVert", card);
            };
        }

        [HttpPost]
        public ActionResult Edit(Scorecard card)
        {
            using (var ctx = new TheCupContext())
            {
                Scorecard dbCard = ctx.Scorecards.Where(c => c.Id == card.Id).First();

                // transfer scores
                for (int i = 1; i < 19; i++)
                {
                    dbCard.SetPuttsForHole(i, card.GetPuttsForHole(i));
                    dbCard.SetStrokesForHole(i, card.GetStrokesForHole(i));
                }
                
                // save here
                ctx.SaveChanges();

                // update references
                ctx.Entry(dbCard).Reference("Tee").Load();
                ctx.Entry(dbCard).Reference("Player").Load();
                ctx.Entry(dbCard).Reference("Round").Load();

                return PartialView("_RoundScoreRow", dbCard);
            }
        }

        public ActionResult Create(int playerId, int roundId, int teeId)
        {
            using (var ctx = new TheCupContext())
            {
                Player p = ctx.Players.Find(playerId);
                Round round = ctx.Rounds.Find(roundId);
                Tee tee = ctx.Tees.Find(teeId);

                Scorecard card = new Scorecard { Round = round, Player = p, Tee = tee };
                card.Handicap = p.Hcp;

                for (int i = 1; i < 19; i++)
                {
                    // set some defaults
                    card.SetPuttsForHole(i, 2);
                    card.SetStrokesForHole(i, card.Tee.GetPar(i) + card.GetAssignedStrokes(i));
                }

                return View(card);
            };
        }

        [HttpPost]
        public ActionResult Create(Scorecard card)
        {
            if (ModelState.IsValid)
            {
                using (var ctx = new TheCupContext())
                {
                    ctx.Scorecards.Add(card);
                    ctx.SaveChanges();

                    ctx.Entry(card).Reference("Tee").Load();
                    ctx.Entry(card).Reference("Player").Load();
                    ctx.Entry(card).Reference("Round").Load();
                }

                return PartialView("_RoundScoreRow", card);
            }

            return HttpNotFound("failed to add scorecard");
        }

        [Authorize]
        public ActionResult Vertical(int id)
        {
            using (var ctx = new TheCupContext())
            {
                var score = ctx.Scorecards.Where(s => s.Id == id).Include(t => t.Tee).Include(r => r.Round).Include(p => p.Player).First();
                return PartialView("_ScoreVert", score);
            }
        }

        //[Authorize]
        //public ActionResult Horizontal(int id)
        //{
        //    using (var ctx = new TheCupContext())
        //    {
        //        var score = ctx.Scorecards.Find(id);
        //        return PartialView("_ScoreHoriz", score);
        //    }
        //}
    }
}
